#ifndef _DVR_PROTOCOL_INFO_H_
#define _DVR_PROTOCOL_INFO_H_

#include <stdint.h>

//应用协议头
#pragma pack(push)
#pragma pack(1)
typedef struct _DVR_APP_HEAD_
{
	uint16_t appFlag;   //默认 0xAAEE   4byte
	uint32_t dataLen;   //总的数据长度  4byte
	char     cmd_main;  //主命令
	char     cmd_sub;   //次命令
}DVR_APP_HEAD,*PDATA_APP_HEAD;

//心跳包
typedef struct _DVR_HEART_BEAT_
{
	uint8_t  UIRequest;
	uint8_t  reserved[3];
	uint16_t year;
	uint16_t day;;
	uint16_t hour;
	uint16_t minute;
	uint16_t second;
}DVR_HEART_BEAT,* PDVR_HEART_BEAT;

const int  DVR_PKT_HEAD_LEN  =  sizeof(DVR_APP_HEAD);

const int  DVR_HEART_PKT_LEN  = sizeof(DVR_HEART_BEAT);

#pragma pack(pop)

typedef struct _DVR_APP_DATA_
{
	DVR_APP_HEAD appHead;
	char *       dataBuf;
	uint32_t     crc32_vol;
}DVR_APP_DATA,*PDVR_APP_DATA;


#define DVR_APP_TRANS_FLAG			0xAAEE
#define DVR_APP_DATA_POS            8   //数据包偏移

#define FILE_PATH_NOR               0
#define FILE_PATH_EVT               1
#define FILE_PATH_PTH               2
#define FILE_PATH_ALL               3

#define FILE_TYPE_NOR               0
#define FILE_TYPE_EVT               1
#define FILE_TYPE_PHO               2
#define FILE_TYPE_D1                3
#define FILE_TYPE_ALL               4

#define HEARTBEAT_UIREQ_PREVIEW     0
#define HEARTBEAT_UIREQ_FILES       1
#define HEARTBEAT_UIREQ_CONFIG      2
#define HEARTBEAT_UIREQ_PLAYBACK    3

// #define APP_DATA_START              0xEE
// #define APP_DATA_END                0xAA

#define FILE_DIR_NOR                0
#define FILE_DIR_EVT                1
#define FILE_DIR_PHO                2

#define RECV_BUFF_LEN               4096
struct app_cmd_transport
{
	int sock_fd;
	int conn_fd;
	char recv_sock[RECV_BUFF_LEN];
	char data_packet[RECV_BUFF_LEN];
	int data_len;
	int cur_read;
	int cur_write;
	int all_len;
};

typedef enum _EM_CONNECT_CMD
{
	CONNECTION_CMD              = 0x00,
	UI_CONTROL_CMD              = 0x01,
	FAST_CONTROL_CMD            = 0x02,
	FILE_CONTROL_CMD            = 0x10,
	CONFIG_GET_CMD              = 0x11,
	CONFIG_SET_CMD              = 0x12,
	DISK_MANAGE_CMD             = 0x18, //磁盘管理
	//扩展回放
	PLAYBACK_CMD                = 0x20, //
	//实时预览控制
	LIVE_CTRL                   = 0x21,

}EM_CONNECT_CMD;

/* CONNECTION_CMD 0x00 */
#define CONN_SUB_HANDSHAKE              0x00
#define CONN_SUB_PAIRING_REQS           0x01
#define CONN_SUB_PAIRING_CHECK          0x02
#define CONN_SUB_HEARTBEAT              0x10

//客户端心跳请求包
typedef struct _HEART_BEAT_REQ_
{
	uint8_t UIRequest;   //0-preview,1-files,2-config,3-playback
	uint8_t reserved[3];
	uint16_t year;
	uint16_t month;
	uint16_t day;
	uint16_t hour;
	uint16_t minute;
	uint16_t second;
}HEART_BEAT_REQ;

//服务端返回心跳包数据
typedef struct _HEART_BEAT_RET_
{
	uint8_t UIStatus;     //0-preview,1-files,2-config,3-playback
	uint8_t CycleRec; //0-stopped,1-starting,2-start,3-stopping
	uint8_t EvtRecStat;   //0-stopped,1-starting,2-start,3-stopping
	uint8_t audioRecStat; //0-close,1-open
	uint8_t sdcordStat;   //0-normal,1-not insert,2-format error,3-space too small,4-write failed,5-formatting
	uint8_t resolution;   //0-1080P, 1-720P
	uint8_t duration;     //Cycle recording duration, uint minutes.
	uint8_t home;         //0-disable, 1-Home(Home为1时，表示APP需返回Home界面)
}HEART_BEAT_RET;

#define CONN_SUB_AP_CONNECT_REQS        0x20

/* UI_CONTROL_CMD 0x01 */
#define UI_CTL_REQ_PREVIEW              0x00
#define UI_CTL_REQ_FILES                0x01
#define UI_CTL_REQ_CONFIG               0x02
#define UI_CTL_REQ_PLAYBACK             0x03

/* FAST_CONTROL_CMD 0x02 */
#define FAST_CTL_CYCLE_RECORD           0x00
#define FAST_CTL_EMERGE                 0x10
#define FAST_CTL_PHOTOGRAPHY            0x11

#define FAST_CTL_LIVE_CH				0x20

#define FAST_CTL_SET_TIME				0x21
typedef struct _TIME_CFG_
{
	uint32_t time;                      //utc时间
	uint32_t time_zone;                 //时区
}TIME_CFG;

#define FAST_CTL_GET_STREAM				0x16
#define FAST_CTL_REC_SWITCH             0x22 //录像开关
#define FAST_CTL_EVENT_RECORD           0x30 //事件录像
typedef struct _EVENT_RECROD_
{
	int ch;
	int event_id;
	int prepare_sec;
	int keep_sec;
}EVENT_RECROD;
typedef struct _RECORD_INFO_
{
	uint32_t channel;
	uint32_t isEnable;
}RECORD_INFO_S;
typedef struct REC_INFO
{
	uint32_t channel;
	uint32_t isEnable;
}REC_INFO;
#define FAST_CTL_REC_STOP               0x88 //录像模块停止
#define FAST_CTL_EVENT_RECORD           0x30 //事件录像
/* FILE_CONTROL_CMD 0x10 */
#define FILE_CTL_GET_DISK_INFO          0x00
#define FILE_CTL_GET_FILE_LIST          0x02
#define FILE_CTL_LOCK_FILE              0x03
#define FILE_CTL_UNLOCK_FILE            0x04
#define FILE_CTL_DELETE					0x05

#define FILE_CTL_REQ_FILE_DELETE        0x21
#define FILE_CTL_REQ_THUMB_OFFSET       0x23
#define FILE_CTL_REQ_FILE_DATE          0x24
#define FILE_CTL_REQ_FILE_SAVING        0x22
#define FILE_CTL_REQ_BACKUP             0x30

//GET_SDCARD_STATUS 状态返回
typedef struct _SDCARD_ST_
{
	uint8_t	  sdcardStatus;  //	0-normal,1-not insert,2-format error,3-space too small,4-write failed,5-formatting
	uint8_t	  sdcardFormat;  //	0-FAT32,1-exFAT,others-not support
	uint16_t  reserved;    //
	uint32_t  total;       //	unit KB
	uint32_t  used;        //	unit KB
	uint32_t  totalFree;   //	unit KB
	uint32_t  cycleUsed;   //	CycleUsed	unit KB
	uint32_t  eventUsed;   //	unit KB
	uint32_t  imageUsed;   //	unit KB
	uint8_t	  cyclePercent;//	unit %
	uint8_t   eventPercent;//	unit %
	uint8_t	  imagePercent;//	unit %
}SDCARD_ST_RET;

//FILE_CTL_GET_FILE_LIST 请求
//typedef struct _FILE_LST_REQ_
//{
//	uint32_t tmStart;  //开始时间
//	uint32_t tmEnd;    //结束时间
//	int32_t  chBit;    //通道位
//	int32_t	 recType;	   //录像类型
//	int32_t  storType;  //
//	int32_t  streamType; //
//	int32_t  diskNo;   //磁盘号
//	uint16_t max_num;  //最大数量
//
//}FILE_LST_REQ;




//单个文件信息
typedef struct _FILE_RET_INFO_
{
	uint16_t	index;     //1 ~ 99999
	uint8_t		path;      //0-/NOR; 1-/EVT; 2-/PHO;
	uint8_t		type;      //0-"NOR_"; 1-"EVT_"; 2-"PHO_"; 3-"_D1_";
	uint8_t		suffix;    //0-".mp4"; 1-".jpg"
	uint8_t		reserved[3];
	uint32_t	size;      //unit bytes
	uint32_t	date;      //File created time from 1970/1/1-0:0:0 ,unit seconds
}FILE_RET_INFO;

//文件列表返回
typedef struct _FILE_LST_RET_
{
	uint32_t	    total_num;			//Number of all valid files in sdcard.
	FILE_RET_INFO f_info[1];			//结构体
}FILE_LST_RET;

//FILE_CTL_REQ_FILE_DELETE
typedef struct _FILE_DEL_INFO_REQ_
{
	uint16_t	fileIndex0;    //1 ~ 99999
	uint16_t	fileIndex1;    //1 ~ 99999
	uint16_t	fileIndex2;    //1 ~ 99999
}FILE_DEL_INFO_REQ;

typedef struct _FILE_DEL_INFO_RET_
{
	uint32_t	del_ret0;    //ERROR_CODE 一次删除三个文件的返回值
	uint32_t	del_ret1;    //ERROR_CODE
	uint32_t	del_ret2;    //ERROR_CODE
}FILE_DEL_INFO_RET;

/* CONFIG_GET_CMD 0x11 */
#define CONFIG_GET_RECORD_CFG           0x00
#define CONFIG_GET_WIFI_CFG             0x01
#define CONFIG_GET_VERSION              0x20

/* CONFIG_SET_CMD 0x12 */
#define CONFIG_SET_RESOLUTION           0x00
#define CONFIG_SET_DURATION             0x01
#define CONFIG_SET_AUDIO_RECORD         0x02
#define CONFIG_SET_OSD                  0x04
#define CONFIG_SET_WIFI_CFG             0x05
#define CONFIG_SET_SDCARD_FMT           0x20
#define CONFIG_SET_FACTORY_RESET        0x21
#define CONFIG_SET_SDCARD_UMOUNT        0x22

//DISK_MANAGE_CMD 磁盘信息管理
#define DISK_INFO_GET                   0x00
#define DISK_FORMAT                     0x01
#define DISK_UMOUNT                     0x02
#define DISK_TEST                       0x03

//回放
#define START_REPLAY                    0x00
#define REPLAY_CTRL                     0x01
#define START_REPLAY_MULCH              0x20
#define STOP_REPLAY                     0x88

typedef struct _PALY_CTRL_
{
	uint8_t contrl_type;
	uint8_t speed; // 对应1078协议上面的FAST_TIME
	uint32_t date_time;  //拖动的时间
	uint32_t ch;    //通道位
}PALY_CTRL_S;

#if 1
#define SEEK_TIME                       0x11
#define FAST_REPLAY                     0x12
#define REBACK_REPLAY                   0x13
#define NORNAL_REPLAY                   0x14
//#define STOP_REPLAY                     0xF8
#endif
//LIVE_CTRL
#define SWITCH_MEDIA                    0x00
#define SEND_CH_BIT                     0x01

typedef struct _PLAY_INFO_
{
	uint32_t channel;
	uint32_t tm_start;
	uint32_t tm_end;

}PLAY_INFO_S;

typedef struct _SEEK_INFO_
{
	uint32_t channel;
	uint32_t seek_time;
}SEEK_INFO_S;

typedef struct _SEEK_MODE_
{
	uint32_t channel;
	uint32_t speed;
	uint32_t seek_time;
}SEEK_MODE_S;

typedef struct _SEEK_NORMAL_
{
	uint32_t channel;
	uint32_t seek_time;
}SEEK_NORMAL_S;

typedef struct _st_heartbeat_time
{
	unsigned short heart_year;
	unsigned short heart_month;
	unsigned short heart_day;
	unsigned short heart_hour;
	unsigned short heart_minute;
	unsigned short heart_second;
	unsigned char  ui_request;
	unsigned char  reserved[3];
}heartbeat_time;

typedef struct _st_fileinfo_
{
	unsigned int  file_index;           /* 1~99999 */
	unsigned char file_path;            /* 0-/NOR; 1-/EVT; 2-/PHO */
	unsigned char file_type;            /* 0-"NOR_"; 1-"EVT_"; 2-"PHO_"; 3-"_D1_" */
	unsigned char file_suffix;          /* 0-".mp4"; 1-".jpg" */
	unsigned char file_reserved;
	unsigned int  file_size;
	unsigned int  file_date;
	unsigned int  file_offset;
}get_fileinfo;

#define FILE_MAX_CNT 65536

typedef struct dcf_ref_elm_s {
	unsigned int datetime;
	char short_fn[15 + 4  + 1 + 1];
	unsigned short pnext;               /* pointer to next element by array index */
	unsigned short pprev;               /* pointer to previous element by array index */
	unsigned char obj_type;
	unsigned char subdir_idx;           /* subdir index; 0 for first-level dir */
} dcf_ref_elm_t;

typedef struct _st_app_file_list
{
	dcf_ref_elm_t dcf_info;
	get_fileinfo  file_info;
}app_file_list;

#define APP_SVR_PORT 7878
#define APP_MEDIA_SVR_PORT 5666
#define APP_MEDIA_SVR_RCV_PORT 5888
//协议错误码
typedef enum _ERR_CODE_
{
	ER_OK         = 0x00000000U,
	ER_PARA_CONST = 0x00000001U,
	ER_PARA_RANGE =	0x00000002U,
	ER_PARA_VALUE = 0x00000004U,
	ER_OVERTIME   = 0x00000008U,
	ER_BUSY	      = 0x00000010U,
	ER_NOT_INIT   =	0x00000020U,
	ER_NOT_SUPPORT=	0x00000040U,
	ER_BUFF_EMPTY =	0x00000080U,
	ER_BUFF_FULL  =	0x00000100U,
	ER_HW_PER	  = 0x00000200U,
	ER_HW_IC	  = 0x00000400U,
	ER_ACCESS	  = 0x00000800U,
	ER_CHECK	  = 0x00001000U,
	ER_BUS_OFF	  = 0x00002000U,
	ER_ABORT	  = 0x00004000U,
	ER_OVERFLOW	  = 0x00008000U,
	ER_UNKNOW	  = 0x80000000U,
}APP_ERR_CDOE;

#endif
